#!/usr/bin/env python
from setup_utils import *
import os

# ijp

def uninstall():
    app = actions.getAppName("ijp.server")
    if app: actions.asadmin("undeploy " + app)
    actions.asadmin("delete-jdbc-resource jdbc/ijp", tolerant=True)
    actions.asadmin("delete-jdbc-connection-pool ijp", tolerant=True)


actions, options, arg = getActions()
props = actions.getGlassfish("ijp-setup.properties", ["dbProperties", "glassfish", "driver", "port"])

prop_name = "ijp.properties"
prop_list = ["icat.url", "ids.url", "reader"]

if arg in ["CONFIGURE", "INSTALL"]:
    actions.configure(prop_name, prop_list, dir="ijp")
    ijpProperties = actions.getProperties(os.path.join("ijp", prop_name), prop_list)
    actions.checkNoErrors()
    
if arg == "INSTALL":   
    actions.installDir("ijp")
    
    try:
        uninstall()
              
        actions.asadmin('create-jdbc-connection-pool --datasourceclassname ' + props["driver"] + 
                ' --restype javax.sql.DataSource --failconnection=true --steadypoolsize 2' + 
                ' --maxpoolsize 8 --ping --property ' + props["dbProperties"] + ' ijp', printOutput=True)
        
        actions.asadmin("create-jdbc-resource --connectionpoolid ijp jdbc/ijp")
      
        actions.deploy("ijp.server-*.war", deploymentorder=140)
                    
    except Exception, e:
        abort(str(e))
               
if arg == "UNINSTALL":
    actions.removeDir("ijp")
    
    try:
        uninstall()
    except Exception, e:
        abort(str(e))       
